/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jardownloader;

/**
 *
 * @author BB
 */
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Downloader {

    String path;

    Downloader(String path) {
        this.path = path;
    }

    void downloadFile(String url_str, OutputStream os)
            throws IllegalStateException, MalformedURLException,
            ProtocolException, IOException {

        URL url = new URL(url_str.replace(" ", "%20"));

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");

        conn.connect();

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {

            byte tmp_buffer[] = new byte[4096];

            InputStream is = conn.getInputStream();

            int n;

            while ((n = is.read(tmp_buffer)) > 0) {
                os.write(tmp_buffer, 0, n);
                os.flush();
            }

        } else {
            throw new IllegalStateException("HTTP response: " + responseCode);
        }
    }

    void getFile() {
        try {
            FileOutputStream fos = null;
            File f = new File(path);
            //make backup
            File outputFile = new File(path + ".backup");
            InputStream in = new FileInputStream(f);
            OutputStream out = new FileOutputStream(outputFile);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            //download new file
            if (f.delete()) {
                try {
                    final String url = "http://dl.dropbox.com/u/30341483/Minecraft/Normal/minecraft.jar";
                    fos = new FileOutputStream(path);
                    downloadFile(url, fos);
                    fos.close();
                } catch (IllegalStateException ex) {
                    Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ProtocolException ex) {
                    Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        fos.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else {
                Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, "Konnte Datei nicht loeschen!");
            }
        } catch (IOException ex) {
            Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
        }



    }
}
